﻿using Frameworks;
using Network.FrameSync;
using Network.FrameSync.Client;
using System.Collections.Generic;

namespace Client
{
    public class XJGameManager : Singleton<XJGameManager>
    {
        public FSPManager Fsp { private set; get; }

        public List<PlayerData> players { private set; get; }

        private XJGameManager() { }

        public override void OnSingletonInit()
        {
            base.OnSingletonInit();

            Init();
        }

        public override void Dispose()
        {
            Fsp.Stop();

            base.Dispose();
        }

        public void Init()
        {
            Fsp = new FSPManager();

            Fsp.onGameBegin += onGameBegin;
            Fsp.onRoundBegin += onRoundBegin;
            Fsp.onControlStart += onControlStart;
            Fsp.onRoundEnd += onRoundEnd;
            Fsp.onGameEnd += onGameEnd;
            Fsp.onGameExit += onGameExit;

            Fsp.SetFrameListener(enterFrame);

            players = new List<PlayerData>();

            GameManager.Instance.AddFixedUpdateListener(onUpdate);
        }


        private void onUpdate()
        {
            Fsp.Tick();
        }

        private void enterFrame(int frame, FSPFrame fSPFrame)
        {
            //UnityEngine.Debug.Log("当前帧:" + frame);
        }

        public void Start(XJGameStartParam param)
        {
            players = param.players;

            Fsp.Start(param.fspParam, OnlineManager.Instance.MainUserData.id);
        }

        public void GameBegin()
        {
            Fsp.SendGameBegin();
            UnityEngine.Debug.Log("GameBegin");
        }

        public void RoundBegin()
        {
            Fsp.SendRoundBegin();
            UnityEngine.Debug.Log("RoundBegin");
        }

        public void ControlStart()
        {
            Fsp.SendControlStart();
            UnityEngine.Debug.Log("ControlStart");
        }

        public void RoundEnd()
        {
            Fsp.SendRoundEnd();
            UnityEngine.Debug.Log("RoundEnd");
        }

        public void GameEnd()
        {
            Fsp.SendGameEnd();
            UnityEngine.Debug.Log("GameEnd");
        }

        public void GameExit()
        {
            Fsp.SendGameExit();
            UnityEngine.Debug.Log("GameExit");
        }


        private void onGameBegin(int frameId)
        {
            UnityEngine.Debug.Log("onGameBegin");

            // 自动开始回合
            RoundBegin();
        }

        private void onRoundBegin(int frameId)
        {
            UnityEngine.Debug.Log("onRoundBegin");

            // 加载场景资源
            GameManager.SceneMgr.LoadScene("Xiangjiang");
        }

        private void onControlStart(int frameId)
        {
            UnityEngine.Debug.Log("onControlStart");
        }

        private void onRoundEnd(int frameId)
        {
            UnityEngine.Debug.Log("onRoundEnd");
        }

        private void onGameEnd(int frameId)
        {
            UnityEngine.Debug.Log("onGameEnd");
        }

        private void onGameExit(uint id)
        {
            UnityEngine.Debug.Log("onGameExit");
        }
    }
}